<template>
  <div>
    <el-button icon="el-icon-plus" size="mini" @click="openDialog">请选择群聊</el-button>
    <el-dialog :visible.sync="open" width="690px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="群名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入群名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
      <el-table v-loading="loading" :data="groupchatList" @selection-change="handleSelectionChange"  @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分组id" align="center" prop="id" v-if="false"/>
      <el-table-column label="群名称" align="left" prop="name" width="240"/>
      <el-table-column label="群主" align="left" prop="owner.name" width="120"/>
      <el-table-column label="群人数" align="center" prop="memberNum"/>
      <el-table-column label="群创建时间" align="center" prop="createAt" width="180" sortable="custom" :sort-orders="['descending', 'ascending']" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
      <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {listGroupchat} from "@/api/wework/groupchat"
export default {
  name: "GroupchatSelect",
  components: {},
  data(){
    return{
      // 遮罩层
      loading: true,
      open: false,
      // 选中数组
      ids: [],
      selectedGroupchatList:[],
      // 选中的群
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 客户群状态字典
      statusOptions: [],
      // 总条数
      total: 0,
      // 客户群列表表格数据
      groupchatList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
    }
  },
  created() {
    this.getList();
    this.getDicts("groupchat_wx_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods:{
    getList(){
      this.loading = true;
      listGroupchat(this.queryParams).then(response => {
        this.groupchatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 客户群状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.selectedGroupchatList = selection;
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    submit(){
      this.open = false;
      this.$emit('selected',this.selectedGroupchatList)
      document.body.click()
    },
    openDialog(){
      // this.reset();
      this.open = true;
      this.title = "请选择要添加的群";
    },
  }
}
</script>
